$('#easySearch').click(function(){
	var mdName = $('#easy_mdname').val().replace(/\s/g, "");
	var keyword = "";
	var summary = "";
	var buildUnit = "";
	if(mdName==""||mdName==null){
		lcl.alert({message: "尚未添加查询条件！",btnok: "我知道了"}).on();
	}
	else{
		var orderby = $('#easy_sj').find("option:selected").val();
		var s={"MetaName":mdName,"keyword":keyword,"abstracts":summary,"buildUnit":buildUnit,"orderby":orderby};
		var str=JSON.stringify(s);//把对象转成字符串
		$.ajax({
			type:"post",
			traditional :true, 
			url:"ArrayController/ComplexSearch",/*复用负责收索，实际上是简单收索*/
			data:{'str':str},
			dataType:"json",
			success:function(res){
				if(res =='0'){
					lcl.alert({message: "查询结果不存在！",btnok: "我知道了"}).on();
				}
				else{
					$('#searchArea').hide();
					$('#resComplex').show();
					var data=new jStringBuffer();
					$('#showSerachResult').html('');
					for(var i=0;i<res.length;i++){
						data.append("<tr id='").append(res[i].owner)
						.append("'><td>").append(i+1).append("</td><td>")
						.append(res[i].objNameC).append("</td><td>")
						.append(res[i].objName).append("</td><td>")
						.append(res[i].tableNameC).append("</td><td>")
						.append(res[i].tableName).append("</td><td>")
						.append(res[i].firstname).append("</td><td>")
						.append(res[i].secondname).append("</td><td>")
						.append(res[i].thirdname).append("</td><td>")
						.append(res[i].unit);//append(res[i].unit).append("</td><td align='center'>" +
					//"<button id='").append(res[i].owner+"|"+res[i].location).append("' type='button' class='btn btn-primary' onclick='showDetail(this)'>详细信息</button>" +
					//"</td></tr>")
					}
					$('#showSerachResult').append(data.toString());
				}
			},
			error:function(){
				lcl.alert({message: "请求失败！",btnok: "我知道了"}).on();
			}
		});
	}
});


$('#complexSearch').click(function(){
	var MetaName = $('#Metaname').val().replace(/\s/g, "");
	var keyword = $('#keyword').val().replace(/\s/g, "");
	var abstracts = $('#abstracts').val().replace(/\s/g, "");
	var buildUnit = $('#buildUnit').val().replace(/\s/g, "");
	if(MetaName==""&&keyword==""&&abstracts==""&&buildUnit==""){
		lcl.alert({message:"尚未添加查询条件！",btnok: "我知道了"}).on();
	}
	else{
		var orderby = $('#sj').find("option:selected").val();
		var s={"MetaName":MetaName,"keyword":keyword,"abstracts":abstracts,"buildUnit":buildUnit,"orderby":orderby};
		var str=JSON.stringify(s);//把对象转成字符串
		$.ajax({
			type:"post",
			traditional :true, 
			url:"ArrayController/ComplexSearch",
			data:{'str':str},
			dataType:"json",
			success:function(res){
				if(res =='0'){
					lcl.alert({message: "查询结果不存在！",btnok: "我知道了"}).on();
				}
				else{
					$('#searchArea').hide();
					$('#resComplex').show();
					var data=new jStringBuffer();
					$('#showSerachResult').html('');
					for(var i=0;i<res.length;i++){
						data.append("<tr id='").append(res[i].owner)
						.append("'><td>").append(i+1).append("</td><td>")
						.append(res[i].objNameC).append("</td><td>")
						.append(res[i].objName).append("</td><td>")
						.append(res[i].tableNameC).append("</td><td>")
						.append(res[i].tableName).append("</td><td>")
						.append(res[i].firstname).append("</td><td>")
						.append(res[i].secondname).append("</td><td>")
						.append(res[i].thirdname).append("</td><td>")
						.append(res[i].unit);
					}
					$('#showSerachResult').append(data.toString());
				}
			},
			error:function(){
				lcl.alert({message: "请求失败！",btnok: "我知道了"}).on();
			}
		});
	}
});

$('#goback').click(function(){
	 $("#searchArea").show();
	 $("#resComplex").hide();
	
});
$('#goback2').click(function(){
	 $("#searchArea").show();
	 $("#resComplex").hide();
	 $("#mljg").hide();
	
});
$('#goback3').click(function(){
//	 $("#searchArea").show();
	 $("#resComplex").hide();
	 $("#mljg2").hide();
	 $("#mljg").show();
	
});

var showDetail = function(obj){

	var id=obj.id;
	console.log(id);
	
	$.post("ArrayController/showmetadata",{"id":id},function(data,status){
		data=$.parseJSON(data);
		var libsid=data['libsid'];
		var classdsid=data['classdsid'];
		data=data['metadata'];
		
		$("#resComplex").hide();
		$("#mljg").show();
		
		var table = $('#shows');
		var count=1;
		var ss=null;
		 var sdate = new Date(data.buildDate); 
         var datestr =bu0(sdate.getMonth()+1)+'/'+bu0(sdate.getDate())+'/'+sdate.getFullYear();
         var sdate2 = new Date(libsid.buildDate); 
         var datestr2 =bu0(sdate2.getMonth()+1)+'/'+bu0(sdate2.getDate())+'/'+sdate2.getFullYear();
         var sdate3 = new Date(libsid.startTime); 
         var datestr3 =bu0(sdate3.getMonth()+1)+'/'+bu0(sdate3.getDate())+'/'+sdate3.getFullYear();
         
		console.log("lv3 "+data);
		table.html('');
		table.append('<tr  id="focusme" align="center"><td colspan=5>'+'元数据信息'+'</td></tr>');
		table.append('<tr><td colspan=2>元数据编码 </td><td colspan=3>'+data['mdCode']+'</td></tr>');
		table.append('<tr><td>元数据名称</td><td>'+data['mdName']+'</td><td>地区编码 </td><td colspan=2>'+data['areaCod']+'</td></tr>');
		table.append('<tr><td>元数据创建单位</td><td>'+data['buildUnit']+'</td><td>创建人</td><td colspan=2>'+data['staff']+'</td></tr>');
		table.append('<tr><td>创建日期</td><td>'+datestr+'</td><td>元数据标准 </td><td colspan=2>'+data['standName']+'</td></tr>');
		table.append('<tr><td>一级分类编码</td><td>'+data['firstlev']+'</td><td>二级分类编码</td><td colspan=2>'+data['secondlev']+'</td></tr>');
		table.append('<tr><td>三级分类编码</td><td>'+data['thirdlev']+'</td><td>绑定数据库owner</td><td colspan=2>'+data['owner']+'</td></tr>');
		table.append('<tr><td colspan=2>绑定数据库URL</td><td colspan=3>'+data['location']+'</td></tr>');
		table.append('<tr  align="center"><td colspan=5>'+'绑定数据库信息'+'</td></tr>');
		table.append('<tr><td>数据库创建单位</td><td>'+libsid['buildUnit']+'</td><td>创建人</td><td colspan=2>'+libsid['staff']+'</td></tr>');
		table.append('<tr><td>数据库管理单位</td><td>'+libsid['buildUnit']+'</td><td>管理人</td><td colspan=2>'+libsid['staff']+'</td></tr>');
		table.append('<tr><td>数据库创建时间</td><td>'+datestr2+'</td><td>数据集覆盖起始时间</td><td colspan=2>'+datestr3+'</td></tr>');
		table.append('<tr><td>关键词</td><td>'+libsid['keywords']+'</td><td>备注</td><td colspan=2>'+libsid['beizhu']+'</td></tr>');
		table.append('<tr><td>建库标准</td><td>'+libsid['standname']+'</td><td>安全级别</td><td colspan=2>'+libsid['seclevl']+'</td></tr>');
		table.append('<tr><td>使用限制</td><td>'+libsid['useconstr']+'</td><td>其他限制</td><td colspan=2>'+libsid['otherconstr']+'</td></tr>');
		table.append('<tr><td>注意事项</td><td>'+libsid['attention']+'</td><td>数据交换环境</td><td colspan=2>'+libsid['exchangeEnv']+'</td></tr>');
		table.append('<tr><td>字符集</td><td>'+libsid['charSet']+'</td><td>数据库服务器访问说明</td><td colspan=2>'+libsid['service']+'</td></tr>');
		table.append('<tr><td colspan=2>数据库版本</td><td colspan=3>'+libsid['version']+'</td></tr>');
		table.append('<tr  id="focusme" align="center"><td colspan=5>'+'表信息'+'</td></tr>');
		console.log(classdsid);
		for(var i=0;i<classdsid.length;i++)
			{var d=classdsid[i];
			table.append('<tr><td>表英文名</td><td>'+d.dsName+'</td><td>表中文名</td><td>'+d.beizhu+'</td><td>'+'<button type="button" id='+d.dsCode+'  class="btn btn-primary btn-xs" onclick="showtablesandcol(id);">查看'+'</td><tr>');
			}
			});
}

var showTables = function(obj){
	var dsCode=obj.id;
	$.post("",{dsCode:dsCode},function(){
		
	});
}


function closed() {
	$("#mljg").hide();
	$("#ml").show();

}


var bu0 = function(number){
    if (number<10){
        return '0'+number;
    }
    return number;
}


function showtablesandcol(id){

	$.get("showtablesAndcolunms",{"id":id},function(data,status){
		data=$.parseJSON(data);
		$("#ml").hide();
		$("#mljg").hide();
		$("#mljg2").show();
		var objsid=data['objdsid'];
		var classsid=data['classdsid'];
		var table = $('#shows2');
		 var sdate = new Date(classsid.buildDate); 
         var datestr =bu0(sdate.getMonth()+1)+'/'+bu0(sdate.getDate())+'/'+sdate.getFullYear();
	//	console.log(objsid);
		table.html('');
		table.append('<tr  align="center"><td colspan=4>'+'表信息'+'</td></tr>');
		table.append('<tr><td>表英文名</td><td>'+classsid.dsName+'</td><td>'+'表中文名'+'</td><td>'+classsid.beizhu+'</td></tr>');
		table.append('<tr><td>创建日期</td><td>'+datestr+'</td><td>'+'关键字'+'</td><td>'+classsid.keywords+'</td></tr>');
		table.append('<tr><td colspan=2>三级分类编码</td><td colspan=2>'+classsid.levelcode+'</td></tr>');
		table.append('<tr  align="center"><td colspan=4>'+'字段信息'+'</td></tr>');
		for(var i=0;i<objsid.length;i++){
			var d = objsid[i];
			 var sdate = new Date(d.buildDate); 
	         var datestr =bu0(sdate.getMonth()+1)+'/'+bu0(sdate.getDate())+'/'+sdate.getFullYear();
	         table.append('<tr><td>字段英文名</td><td>'+d.dsName+'</td><td>'+'字段中文名'+'</td><td>'+d.beizhu+'</td></tr>');
				table.append('<tr><td>创建日期</td><td>'+datestr+'</td><td>'+'记录变更备注'+'</td><td>'+d.comments+'</td></tr>');
				table.append('<tr><td >数据类型</td><td >'+d.dataType+'</td><td>'+'是否为主键'+'</td><td>'+d.ispk+'</td></tr>');
				
	//	console.log(d);
		}
	});

}